import { RuntimeError } from '@ibiz-template/core';
import { IExcelUtil } from '../../interface';
import { UIActionUtil } from '../../ui-action';
import {
  TextUtil,
  LayoutPanelUtil,
  HandlebarsUtil,
  RawValueUtil,
  ThemeUtil,
  DefaultErrorHandler,
  ErrorHandlerCenter,
  ViewStack,
  AnimeUtil,
  FileUtil,
  ShortCutUtil,
  BIReportUtil,
  RecordNavUtil,
  JsonSchemaUtil,
} from '../../utils';

// 保存浏览器标签原始标题
// const sourceTitle = document.title;

/**
 * 全局工具方法或对象
 *
 * @author chitanda
 * @date 2023-04-27 21:04:32
 * @export
 * @class GlobalUtil
 */
export class GlobalUtil {
  /**
   * 布局面板
   *
   * @author chitanda
   * @date 2023-04-27 21:04:46
   */
  readonly layoutPanel = new LayoutPanelUtil();

  /**
   * 主题设置工具
   *
   * @author chitanda
   * @date 2023-12-02 23:12:27
   */
  readonly theme = new ThemeUtil();

  /**
   * 文本工具
   *
   * @author zhanghengfeng
   * @date 2023-08-24 11:08:28
   */
  readonly text = new TextUtil();

  /**
   * handlebars 工具
   *
   * @author chitanda
   * @date 2023-08-28 23:08:59
   */
  readonly hbs = new HandlebarsUtil();

  /**
   * 直接值工具
   *
   * @author zhujiamin
   * @date 2023-08-24 11:08:28
   */
  readonly rawValue = new RawValueUtil();

  /**
   * 执行界面行为
   *
   * @author chitanda
   * @date 2023-11-28 19:11:26
   */
  readonly action = UIActionUtil;

  /**
   * 错误处理中心
   * @author lxm
   * @date 2023-09-26 05:04:26
   */
  readonly error = new ErrorHandlerCenter();

  /**
   * 视图堆栈
   *
   * @author chitanda
   * @date 2024-01-18 14:01:23
   */
  readonly viewStack = new ViewStack();

  /**
   * 动画工具类
   *
   * @author zk
   * @date 2024-01-22 09:01:33
   * @memberof GlobalUtil
   */
  readonly anime = new AnimeUtil();

  /**
   *  最小化工具类
   *
   * @author fzh
   * @date 2024-04-22 09:01:33
   * @memberof GlobalUtil
   */
  readonly shortCut = new ShortCutUtil();

  /**
   * 文件工具类
   *
   * @author zk
   * @date 2024-01-26 04:01:24
   * @memberof GlobalUtil
   */
  readonly file = new FileUtil();

  /**
   * bi报表工具类
   *
   * @author tony001
   * @date 2024-06-30 11:06:13
   */
  readonly biReport = new BIReportUtil();

  /**
   * 记录导航工具类
   *
   * @author tony001
   * @date 2024-07-15 13:07:51
   */
  readonly record = new RecordNavUtil();

  /**
   * JsonSchema工具类
   *
   * @author tony001
   * @date 2024-07-25 00:07:31
   */
  readonly jsonSchema = new JsonSchemaUtil();

  constructor() {
    this.error.register(new DefaultErrorHandler());
  }

  /**
   * 获取导出Excel工具类对象
   * @author lxm
   * @date 2023-08-24 10:47:06
   */
  getExcelUtil?: () => Promise<IExcelUtil>;

  /**
   * 显示应用级别的加载提示
   *
   * @author chitanda
   * @date 2023-09-08 10:09:43
   */
  showAppLoading(): void {
    // const el = document.getElementById('app-loading-x') as HTMLDivElement;
    // if (el) {
    //   el.style.display = 'none';
    // }
  }

  /**
   * 隐藏应用级别的加载提示
   *
   * @author chitanda
   * @date 2023-09-08 10:09:15
   */
  hiddenAppLoading(): void {
    // setTimeout(() => {
    //   const el = document.getElementById('app-loading-x') as HTMLDivElement;
    //   if (el) {
    //     el.style.display = 'none';
    //   }
    // }, 300);
  }

  /**
   * 设置浏览器标签页标题
   *
   * @author chitanda
   * @date 2024-02-05 09:02:08
   * @param {string} title
   */
  setBrowserTitle(title: string): void {
    // const app = ibiz.hub.getApp();
    // let tabTitle: string = '';
    // if (ibiz.env.AppTitle) {
    //   tabTitle = ibiz.env.AppTitle;
    // } else if (app.model.title) {
    //   tabTitle = app.model.title;
    // } else {
    //   tabTitle = sourceTitle;
    // }
    // if (title) {
    //   document.title = `${tabTitle} - ${title}`;
    // } else {
    //   document.title = tabTitle;
    // }
  }

  /**
   * 获取应用全局变量
   * @author lxm
   * @date 2024-04-01 11:24:58
   * @return {*}  {IParams}
   */
  getGlobalParam(): IParams {
    throw new RuntimeError(ibiz.i18n.t('runtime.global.noImplemented'));
  }

  /**
   * 获取视图路由参数变量，数组类型，基于路由解析出来的，每一个对象里面都有context和params
   * @author lxm
   * @date 2024-04-01 11:24:58
   * @return {*}  {IParams}
   */
  getRouterParams(): IParams[] {
    throw new RuntimeError(ibiz.i18n.t('runtime.global.noImplementedRouting'));
  }
}
